package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.db.DatabaseUtil;

public class NewSongMenuActivity extends Activity{
	private EditText newMenuTxt ;
	private Button   newMenuYes;
	private Button   newMenuNo;
	private TextView newMenuNameText;
	private ProgressDialog pd;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_song_menu_lay);
		init();
	}
	
	private void init(){
		newMenuTxt = (EditText) findViewById(R.id.new_menu_txt); 
		newMenuYes = (Button) findViewById(R.id.new_menu_yes);
		newMenuNo  = (Button) findViewById(R.id.new_menu_no);
		newMenuNameText = (TextView) findViewById(R.id.nameText);
		newMenuNo.setOnClickListener(onClickListener);
		newMenuYes.setOnClickListener(onClickListener);
		
		if(!NativeMusicManageActivity.menu_name.equals("")){
			newMenuNameText.setText("重命名");
			newMenuTxt.setText(NativeMusicManageActivity.menu_name);
		}
	}

	private View.OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.new_menu_yes: {
				String menumName = newMenuTxt.getText().toString();
				//更新歌单名
				if(!NativeMusicManageActivity.menu_name.equals("")){
					DatabaseUtil databaseUtil = new DatabaseUtil(NewSongMenuActivity.this);
					List<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
					arrayList = databaseUtil.queryMusicMenu();
					for(int  i =0; i<arrayList.size();i++){
						if(arrayList.get(i).get("name").equals(menumName)){
							Toast.makeText(NewSongMenuActivity.this,
									" 歌单名不能重复", Toast.LENGTH_SHORT)
									.show();
							return ;
						}
					}
					databaseUtil.updateMusicMenu(menumName, NativeMusicManageActivity.menu_name);
					
					Intent intent = new Intent();
					intent.putExtra("name",NativeMusicManageActivity.menu_name );
					intent.putExtra("newName",menumName );
					NativeMusicManageActivity.menu_name = "";
					NewSongMenuActivity.this.setResult(RESULT_OK, intent);
					NewSongMenuActivity.this.finish();
					
					
				}
				
				//新增歌单
				else
				{

					if(menumName.equals("")){
						Toast.makeText(NewSongMenuActivity.this,
								" 歌单名不能为空", Toast.LENGTH_SHORT)
								.show();
					}
					if(!menumName.equals("")){
						DatabaseUtil databaseUtil = new DatabaseUtil(NewSongMenuActivity.this);
						List<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
						arrayList = databaseUtil.queryMusicMenu();
						for(int  i =0; i<arrayList.size();i++){
							if(arrayList.get(i).get("name").equals(menumName)){
								Toast.makeText(NewSongMenuActivity.this,
										" 歌单名不能重复", Toast.LENGTH_SHORT)
										.show();
								return ;
							}
						}
						 if(ServerAccess.AddOneMusicToMenu == false){
							 databaseUtil.insertMenu(menumName, "", "",0,0,"");
							 Intent intent = new Intent(NewSongMenuActivity.this, AddToMenuActivity.class);
							 intent.putExtra("name", menumName);
							 startActivity(intent);
						 }
						 else{
							 databaseUtil.insertMenu(menumName, "", "",0,0,"");
							 String name = menumName;
							 String musicName = ServerAccess.musicMap.get("title").toString();
							 String playerName = ServerAccess.musicMap.get("Artist").toString();
							 Long duration = Long.parseLong(ServerAccess.musicMap.get("duration").toString());
							 Long size = Long.parseLong(ServerAccess.musicMap.get("size").toString());
							 String url = ServerAccess.musicMap.get("url").toString();
							 databaseUtil.insertMenu(name, musicName, playerName, duration, size, url);
							 Toast.makeText(NewSongMenuActivity.this,
									" 添加成功", Toast.LENGTH_SHORT)
									.show();
							finish();
							 
						 }
					}
				}
				
			}
				break;
			case R.id.new_menu_no: {
				finish();
				overridePendingTransition(R.anim.no_move, R.anim.right_out);
			}
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
